#include "cpp-httplib/httplib.h"
#include <regex>
#include "searcher.hpp"

const std::string input = "data/raw_html/raw.txt";
const std::string root = "./wwwroot";
ns_searcher::searcher * mysearcher;
void Handler(const httplib::Request& req, httplib::Response& rep){
    //如果没有携带word参数，说明没有输入需要的关键字
    if(!req.has_param("word")){
        rep.set_content("请输入关键字", "text/plain; charset=utf-8");
        return ;
    }
    //如果携带了关键字
    else{
        //获取查询的关键字
        const std::string word=req.get_param_value("word");
        LOG(NORMAL,"查询关键字："+word);
        std::string json_string;
        mysearcher->Search(word,&json_string);
        rep.set_content(json_string,"application/json");

    }
}
int main()
{
    // 创建查找对象
    mysearcher = new ns_searcher::searcher();
    mysearcher->InitSearcher(input);
    httplib::Server svr;
    //设置web根目录
    svr.set_base_dir(root.c_str());
    svr.Get("/s",Handler);
    LOG(NORMAL,"服务器启动成功");
    svr.listen("0.0.0.0", 8081);
    return 0;
}